
exports.__esModule = true;
exports.default = void 0;

const _utils = require('../utils');

const _constant = require('../utils/constant');

const _createNamespace = (0, _utils.createNamespace)('pagination');
const createComponent = _createNamespace[0];
const bem = _createNamespace[1];
const t = _createNamespace[2];

function makePage(number, text, active) {
  return {
    number,
    text,
    active,
  };
}

const _default = createComponent({
  props: {
    prevText: String,
    nextText: String,
    forceEllipses: Boolean,
    mode: {
      type: String,
      default: 'multi',
    },
    value: {
      type: Number,
      default: 0,
    },
    pageCount: {
      type: [Number, String],
      default: 0,
    },
    totalItems: {
      type: [Number, String],
      default: 0,
    },
    itemsPerPage: {
      type: [Number, String],
      default: 10,
    },
    showPageSize: {
      type: [Number, String],
      default: 5,
    },
  },
  computed: {
    count: function count() {
      const count = this.pageCount || Math.ceil(this.totalItems / this.itemsPerPage);
      return Math.max(1, count);
    },
    pages: function pages() {
      const pages = [];
      const pageCount = this.count;
      const showPageSize = +this.showPageSize;

      if (this.mode !== 'multi') {
        return pages;
      } // Default page limits


      let startPage = 1;
      let endPage = pageCount;
      const isMaxSized = showPageSize < pageCount; // recompute if showPageSize

      if (isMaxSized) {
        // Current page is displayed in the middle of the visible ones
        startPage = Math.max(this.value - Math.floor(showPageSize / 2), 1);
        endPage = startPage + showPageSize - 1; // Adjust if limit is exceeded

        if (endPage > pageCount) {
          endPage = pageCount;
          startPage = endPage - showPageSize + 1;
        }
      } // Add page number links


      for (let number = startPage; number <= endPage; number++) {
        const page = makePage(number, number, number === this.value);
        pages.push(page);
      } // Add links to move between page sets


      if (isMaxSized && showPageSize > 0 && this.forceEllipses) {
        if (startPage > 1) {
          const previousPageSet = makePage(startPage - 1, '...', false);
          pages.unshift(previousPageSet);
        }

        if (endPage < pageCount) {
          const nextPageSet = makePage(endPage + 1, '...', false);
          pages.push(nextPageSet);
        }
      }

      return pages;
    },
  },
  watch: {
    value: {
      handler: function handler(page) {
        this.select(page || this.value);
      },
      immediate: true,
    },
  },
  methods: {
    select: function select(page, emitChange) {
      page = Math.min(this.count, Math.max(1, page));

      if (this.value !== page) {
        this.$emit('input', page);

        if (emitChange) {
          this.$emit('change', page);
        }
      }
    },
  },
  render: function render() {
    const _this = this;

    const h = arguments[0];
    const { value } = this;
    const simple = this.mode !== 'multi';

    const onSelect = function onSelect(value) {
      return function () {
        _this.select(value, true);
      };
    };

    return h('ul', {
      class: bem({
        simple,
      }),
    }, [h('li', {
      class: [bem('item', {
        disabled: value === 1,
      }), bem('prev'), _constant.BORDER],
      on: {
        click: onSelect(value - 1),
      },
    }, [this.prevText || t('prev')]), this.pages.map((page) => h('li', {
      class: [bem('item', {
        active: page.active,
      }), bem('page'), _constant.BORDER],
      on: {
        click: onSelect(page.number),
      },
    }, [page.text])), simple && h('li', {
      class: bem('page-desc'),
    }, [this.slots('pageDesc') || `${value}/${this.count}`]), h('li', {
      class: [bem('item', {
        disabled: value === this.count,
      }), bem('next'), _constant.BORDER],
      on: {
        click: onSelect(value + 1),
      },
    }, [this.nextText || t('next')])]);
  },
});

exports.default = _default;
